package com.flyee.controller.tools.cartoon.ui.changeequip.control
{
	import com.flyee.controller.tools.cartoon.model.CartoonData;
	import com.flyee.controller.tools.cartoon.ui.changeequip.model.TransformCartoonDataModel;
	
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.display.BitmapData;
	public class TransformCartoonDataManager
	{
		private static var instance:TransformCartoonDataManager = null;
		
		private var cartoonDataArray:Array = new Array();
		
		private var isOpen:Boolean = true;
		
		private var transformingModel:TransformCartoonDataModel = null;
		
		private var loadingLoader:Loader = null;
		
		public function TransformCartoonDataManager()
		{
			if (instance)
			{
				throw new Error("singleton error!!!");
			}
			instance = this;
		}
		public static function getInstance():TransformCartoonDataManager
		{

			if (instance == null)
			{
				instance = new TransformCartoonDataManager();
			}
			return instance;
		}
		public function regist(cartoonData:CartoonData,returnFun:Function):void
		{
			var tempModel:TransformCartoonDataModel = new TransformCartoonDataModel();
			tempModel.data = cartoonData.bitmapData;
			tempModel.type = cartoonData.pose;
			tempModel.dir = cartoonData.direct;
			tempModel.frame = cartoonData.frameNumber;
			tempModel.returnFun = returnFun;
			tempModel.x = cartoonData.initX;
			tempModel.y = cartoonData.initY;
			cartoonDataArray.push(tempModel);
			transform();
		}
		public function getListLength():uint
		{
			return this.cartoonDataArray.length;
		}
		public function transform():void
		{
			if(this.isOpen)
			{
				this.isOpen = false;
				if(this.cartoonDataArray.length > 0)
				{
					
					var tempModel:TransformCartoonDataModel = this.cartoonDataArray.shift();
					this.transformingModel = tempModel;
					var tempLoader:Loader = new Loader();
					loadingLoader = tempLoader;
					tempLoader.loadBytes(tempModel.data,new LoaderContext(false,ApplicationDomain.currentDomain));
					tempLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandle);
					tempLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,errorHandle);
				}
				else
				{
					this.isOpen = true;
				}
			}
		}
		public function completeHandle(e:Event):void
		{
			var loader:Loader = e.currentTarget.loader;
			this.transformingModel.bitmapdata = new BitmapData(loader.width,loader.height,true,0x000000);
			this.transformingModel.bitmapdata.draw(loader);
			if(this.transformingModel.returnFun != null)
			{
				this.transformingModel.returnFun(this.transformingModel)
			}
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,completeHandle);
			loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,errorHandle);
			
			this.loadingLoader = null;
			this.transformingModel = null;
			this.isOpen = true;
			this.transform();
		}
		public function errorHandle(e:IOErrorEvent):void
		{
			if(this.transformingModel.returnFun != null)
			{
				this.transformingModel.returnFun(null)
			}
			var loader:Loader = e.currentTarget.loader;
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,completeHandle);
			loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,errorHandle);
			
			this.loadingLoader = null;
			this.transformingModel = null;
			this.isOpen = true;
			this.transform();
		}
	}
}